/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.cs.filter.RuleManager;
/*     */ import com.zimbra.cs.mailbox.DeliveryOptions;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailSender;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.service.FileUploadServlet;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AddMsg
/*     */   extends MailDocumentHandler
/*     */ {
/*  52 */   private static Log mLog = LogFactory.getLog(AddMsg.class);
/*  53 */   private static Pattern sNumeric = Pattern.compile("\\d+");
/*     */   
/*  55 */   private static final String[] TARGET_FOLDER_PATH = { "m", "l" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  59 */     String folder = getXPath(request, TARGET_FOLDER_PATH);
/*  60 */     return (folder != null) && ((folder.indexOf(':') > 0) || (sNumeric.matcher(folder).matches())) ? TARGET_FOLDER_PATH : null;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  65 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  70 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  71 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  72 */     OperationContext octxt = getOperationContext(zsc, context);
/*  73 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*  75 */     Element msgElem = request.getElement("m");
/*     */     
/*  77 */     String flagsStr = msgElem.getAttribute("f", null);
/*  78 */     String[] tags = TagUtil.parseTags(msgElem, mbox, octxt);
/*  79 */     String folderStr = msgElem.getAttribute("l");
/*  80 */     boolean noICal = msgElem.getAttributeBool("noICal", false);
/*  81 */     long date = msgElem.getAttributeLong("d", System.currentTimeMillis());
/*  82 */     boolean filterSent = request.getAttributeBool("filterSent", false);
/*     */     
/*  84 */     if (mLog.isDebugEnabled()) {
/*  85 */       StringBuffer toPrint = new StringBuffer("<AddMsg ");
/*  86 */       if (tags != null) {
/*  87 */         toPrint.append(" tags=\"").append(TagUtil.encodeTags(tags)).append("\"");
/*     */       }
/*  89 */       if (folderStr != null) {
/*  90 */         toPrint.append(" folder=\"").append(folderStr).append("\"");
/*     */       }
/*  92 */       toPrint.append(">");
/*  93 */       mLog.debug(toPrint);
/*     */     }
/*     */     
/*  96 */     int folderId = -1;
/*  97 */     Folder folder = null;
/*     */     try {
/*  99 */       folderId = new ItemId(folderStr, zsc).getId();
/*     */     } catch (ServiceException e) {}
/* 101 */     if (folderId > 0) {
/* 102 */       folder = mbox.getFolderById(octxt, folderId);
/*     */     } else {
/* 104 */       folder = mbox.getFolderByPath(octxt, folderStr);
/* 105 */       folderId = folder.getId();
/*     */     }
/* 107 */     mLog.debug("folder = %s", new Object[] { folder.getName() });
/*     */     
/*     */ 
/* 110 */     String attachment = msgElem.getAttribute("aid", null);
/*     */     
/* 112 */     ParseMimeMessage.MimeMessageData mimeData = new ParseMimeMessage.MimeMessageData();
/*     */     Message msg;
/*     */     try { MimeMessage mm;
/*     */       MimeMessage mm;
/* 116 */       if (attachment != null) {
/* 117 */         mm = SendMsg.parseUploadedMessage(zsc, attachment, mimeData);
/*     */       } else {
/* 119 */         mm = ParseMimeMessage.importMsgSoap(msgElem);
/*     */       }
/*     */       
/* 122 */       int flagsBitMask = Flag.toBitmask(flagsStr);
/*     */       
/* 124 */       ParsedMessage pm = new ParsedMessage(mm, date, mbox.attachmentsIndexingEnabled());
/* 125 */       Message msg; if ((filterSent) && (!DebugConfig.disableOutgoingFilter) && (folderId == MailSender.getSentFolderId(mbox)) && ((flagsBitMask & Flag.BITMASK_FROM_ME) != 0))
/*     */       {
/* 127 */         List<ItemId> addedItemIds = RuleManager.applyRulesToOutgoingMessage(octxt, mbox, pm, folderId, noICal, flagsBitMask, tags, -1);
/*     */         
/*     */ 
/* 130 */         msg = addedItemIds.isEmpty() ? null : mbox.getMessageById(octxt, ((ItemId)addedItemIds.get(0)).getId());
/*     */       } else {
/* 132 */         DeliveryOptions dopt = new DeliveryOptions().setFolderId(folderId).setNoICal(noICal).setFlags(flagsBitMask).setTags(tags);
/* 133 */         msg = mbox.addMessage(octxt, pm, dopt, null);
/*     */       }
/*     */     } catch (IOException ioe) {
/* 136 */       throw ServiceException.FAILURE("Error While Delivering Message", ioe);
/*     */     }
/*     */     finally {
/* 139 */       if (mimeData.fetches != null) {
/* 140 */         FileUploadServlet.deleteUploads(mimeData.fetches);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 145 */     if (mimeData.uploads != null) {
/* 146 */       FileUploadServlet.deleteUploads(mimeData.uploads);
/*     */     }
/*     */     
/* 149 */     Element response = zsc.createElement(MailConstants.ADD_MSG_RESPONSE);
/* 150 */     if (msg != null) {
/* 151 */       ToXML.encodeMessageSummary(response, ifmt, octxt, msg, null, 4195127);
/*     */     }
/*     */     
/* 154 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/AddMsg.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */